#!/usr/bin/env python3
import sys, glob, os
import json

if (len(sys.argv) != 2):
    print('show_result.py "result_dir_pattern"')
    exit(0)

pattern = sys.argv[1]
directories = glob.glob(pattern)

buffer=[]

print('MODEL,TESTSET,ACC,TER,N:HYP:REF:MAX,DATE')
for dir in directories:
    try:
        date, service, test_set, max_utts = dir.split('__')
        with open(os.path.join(dir, 'RESULTS.txt'), 'r', encoding = 'utf8') as f:
            for l in f:
                if 'token_error_rate' in l:
                    r = json.loads(l)
                    TER = r['token_error_rate']
                    R = r['num_ref_utts']
                    H = r['num_hyp_utts']
                    n = r['num_eval_utts']
                    buffer.append(F"{service},{test_set},{100-TER:.2f},{TER:.2f},{n}:{H}:{R}:{max_utts},{date}")
    except:
        continue

buffer.sort()

for l in buffer:
    print(l)
